package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.TaskLogsService;
import lombok.Data;
import org.apache.catalina.util.LifecycleMBeanBase;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/17
 */
@Component
public class PageData21 {

    // TODO: 2020/9/17 未完成

    @Resource
    private TaskLogsService taskLogsService;

    public String getData(ExcelModel model){
        Page21 page21 = new Page21();
        List<String> ytdDay = new ArrayList<>();
        String startDate = "2020.01.01 00:00:00";
        String endDate = "2020.12.31 23:59:59";
        ytdDay.add(startDate);
        ytdDay.add(endDate);

        List<TempBeen> performance = taskLogsService.getPerformance(ytdDay.get(0), ytdDay.get(1));
        long utcRushBuy = taskLogsService.getUtcRushBuy(ytdDay.get(0), ytdDay.get(1));

        TempUtc tempUtc1 = new TempUtc();
        tempUtc1.setUtcId("rushBuy");
        tempUtc1.setUtcName("限时抢购");
        tempUtc1.setUtcCount(utcRushBuy);

        double a1 = 0;
        double a2 = 1;
        for (TempBeen tempBeen : performance) {
            if (tempBeen.getTempData().equals("1")){
                a1 = tempBeen.getTempCount();
            }
            if (tempBeen.getTempData().equals("2")){
                a2 = tempBeen.getTempCount();
            }
        }

        String utcTitle = "UTC整体表现：'"+ StringUtil.decimalFormat(a1/a2*100) +"%'的用户参与过签到";

        List<String> list = Arrays.asList("2", "9", "12", "13", "14");
        List<TempUtc> tempUtcs = new ArrayList<>();
        performance.forEach(been -> {
            //14,13,9,12,2
            String tempData = been.getTempData();
            if (list.contains(tempData)){
                return;
            }
            TempUtc tempUtc = new TempUtc();
            tempUtc.setUtcName(been.getTempName());
            tempUtc.setUtcCount(been.getTempCount());
            tempUtc.setUtcId(tempData);
            tempUtcs.add(tempUtc);
        });
        tempUtcs.add(tempUtc1);
        tempUtcs.sort(Comparator.comparing(TempUtc::getUtcCount));

        List<TempSynchronize> tempSyns = new ArrayList<>();

        TempSynchronize tempSyn1 = new TempSynchronize();
        tempSyn1.setSynName("仅同步过1次UTC积分");
        tempSyn1.setSynCount(taskLogsService.getSynchronize1(ytdDay.get(0), ytdDay.get(1)));
        tempSyns.add(tempSyn1);

        TempSynchronize tempSyn2 = new TempSynchronize();
        tempSyn2.setSynName("同步过2-5次");
        tempSyn2.setSynCount(taskLogsService.getSynchronize2(ytdDay.get(0), ytdDay.get(1)));
        tempSyns.add(tempSyn2);

        TempSynchronize tempSyn3 = new TempSynchronize();
        tempSyn3.setSynName("同步过6-10次");
        tempSyn3.setSynCount(taskLogsService.getSynchronize3(ytdDay.get(0), ytdDay.get(1)));
        tempSyns.add(tempSyn3);

        TempSynchronize tempSyn4 = new TempSynchronize();
        tempSyn4.setSynName("同步过10次以上");
        long synchronize4 = taskLogsService.getSynchronize4(ytdDay.get(0), ytdDay.get(1));
        tempSyn4.setSynCount(synchronize4);
        tempSyns.add(tempSyn4);
        String synTitle = "UTC整体表现：共'"+StringUtil.decimalFormat(synchronize4)+"'位用户领取UTC活动积分超过10次；";

        page21.setTitle(LogicUtil.getTitle(model,21));
        page21.setDataPeriod(DateUtil.getFirstDay2LastSunday());
        page21.setUtcChannel(tempUtcs);
        page21.setSyns(tempSyns);
        page21.setUtcTitle(utcTitle);
        page21.setSynTitle(synTitle);

        return JsonUtil.serialize(page21);
    }

    @Data
    public static class Page21{
        private String title;
        private String dataPeriod;
        private String utcTitle;
        private List<TempUtc> utcChannel;
        private String synTitle;
        private List<TempSynchronize> syns;
    }

    @Data
    public static class TempUtc{
        private String utcName;
        private String utcId;
        private long utcCount;
    }

    @Data
    public static class TempSynchronize{
        private String synName;
        private long synCount;
    }

}
